<div class="filter">
	<select class="filters_col" id="ad_sites" name="sites">
		<option value="">---Sites---</option>
		<?php foreach($filters['site'] as $site): ?>
		<option value="<?php echo $site; ?>">
		<?php echo $site; ?>
		</option>
		<?php endforeach; ?>
	</select> <input type="button" id="ad_filter" value=">>>Filter" />
</div>

<table class="tablesorter" id="tab1">
	<thead>
		<tr>
			<th class="col_date">Date<span class="close">X</span></th>
			<th class="col_placement">placement<span class="close">X</span></th>
			<th class="col_site">Site<span class="close">X</span></th>
			<th class="col_advertiser">advertiser<span class="close">X</span></th>
			<th class="col_imp">impression<span class="close">X</span></th>
			<th class="col_redi">redirect<span class="close">X</span></th>
			<th class="col_click">click<span class="close">X</span></th>
			<th class="col_conv">conversion<span class="close">X</span></th>
			<th class="col_reve">revenue<span class="close">X</span></th>
			<th class="col_ecpm">eCPM<span class="close">X</span></th>
			<th class="col_rpc">RPC<span class="close">X</span></th>
			<th class="col_rpa">RPA<span class="close">X</span></th>
			<th class="col_ctr">CTR<span class="close">X</span></th>
			<th class="col_conv_rate">conversion rate<span class="close">X</span></th>
			<th class="col_prog">progression<span class="close">X</span></th>
		</tr>
	</thead>
	<tbody>
	<?php if((!empty($reportItems)) && $reporttype=="3"): ?>
	<?php foreach ($reportItems as $reportItemArray): ?>
	<?php foreach($reportItemArray as $reportItem):
	if(($reportItem['impression'] == '' || $reportItem['impression'] == 0)
		&& ($reportItem['click'] == '' || $reportItem['click'] == 0)
		&& ($reportItem['conversion'] == '' || $reportItem['conversion'] == 0)
		&& ($reportItem['revenue'] == '' || $reportItem['revenue'] == 0) ){continue;} 
		
		$date['start'] = $reportItem['t'];
		if($interval == 2){
			list($year, $week) = str_split($reportItem['t'], 4);
			$date = Util::getStartAndEndDate($week, $year);
		}
		$conv_rate = '---';
		if(strpos($reportItem['placement'], 'offer wall')){
			$conv_rate = isset($reportItem['conversion']) && isset($reportItem['click'])? Util::divide($reportItem['conversion'], $reportItem['click'], 2) : '---';
		}else if($reportItem['placement'] == 'opt-in'){
			$conv_rate = isset($reportItem['conversion']) && isset($reportItem['impression'])? Util::divide($reportItem['conversion'], $reportItem['impression'], 2) : '---';
		}
	?>
		<tr>
			<td class="col_date"><?php echo $date['start'];?> <?php if($interval==2){ echo '---'.$date['end'];} ?></td>
			<td class="col_placement"><?php echo isset($reportItem['placement'])? $reportItem['placement'] : 0;  ?></td>
			<td class="col_site"><?php echo $reportItem['site'];?></td>
			<td class="col_advertiser"><?php echo isset($reportItem['advertiser'])? $reportItem['advertiser'] : 0;  ?></td>
			<td class="col_imp"><?php echo isset($reportItem['impression'])? $reportItem['impression'] : 0;  ?></td>
			<td class="col_redi"><?php echo isset($reportItem['redirect'])? $reportItem['redirect'] : '---';  ?></td>
			<td class="col_click"><?php echo isset($reportItem['click'])? $reportItem['click'] : '---';  ?></td>
			<td class="col_conv"><?php echo isset($reportItem['conversion'])? $reportItem['conversion'] : '---';  ?></td>
			<td class="col_reve"><?php echo isset($reportItem['revenue'])? $reportItem['revenue'] : '---';  ?></td>
			<td class="col_ecpm"><?php echo (isset($reportItem['revenue']) && isset($reportItem['impression']))? Util::divide($reportItem['revenue'], $reportItem['impression'], 2)*1000 : '---'; ?></td>
			<td class="col_rpc"><?php echo (isset($reportItem['revenue']) && isset($reportItem['click']))? Util::divide($reportItem['revenue'], $reportItem['click'], 2) : '---'; ?></td>
			<td class="col_rpa"><?php echo (isset($reportItem['revenue']) && isset($reportItem['conversion']))? Util::divide($reportItem['revenue'], $reportItem['conversion'], 2) : '---'; ?></td>
			<td class="col_ctr"><?php echo (isset($reportItem['click']) && isset($reportItem['impression']))? Util::divide($reportItem['click'], $reportItem['impression'], 2) : '---'; ?></td>
			<td class="col_conv_rate"><?php echo $conv_rate; ?></td>
			<td class="col_prog"><?php echo (isset($reportItem['redirect']) && isset($reportItem['impression']))? Util::divide($reportItem['redirect'], $reportItem['impression'], 2)*100 . '%' : '---'; ?></td>
		</tr>
		<?php endforeach; ?>
		<?php endforeach;?>
		<?php endif;?>
	</tbody>
</table>

<script type="text/javascript" src="<?php echo JS_DIR; ?>jquery.tablesorter.js"></script>
<script type="text/javascript" src="<?php echo JS_DIR; ?>jquery.uitablefilter.js"></script>
<script type="text/javascript">
	var $table = $('#tab1');
	$table.tablesorter({sortList: [[0,0]]});

	$('#ad_filter').click(function(){
		var id_to_header={
						  "ad_sites":"Site"
					   	 };
		
		var $table = $('#tab1');
		var filters = new Array();
	    var elems = $('.filters_col');
	    elems.each(function(){
			var elem = $(this);
			if(elem.css('display') != 'none'){
				if(elem.val() != ""){
					filters[elem.val()] = id_to_header[elem.attr('id')];
				}
			}
		});
		console.log(filters);
	    $.uiTableFilter( $table, filters);
	});
	
</script>
